﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UngdungComponent.BLL;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace UngdungComponent.DAL
{
    public class UngdungDAL:IUngdungDAL
    {
        #region IUngdung Connection
        private string connectionstring=Commom.WCManager.ConnectionString();

	    public string Connection
	    {
		    get { return connectionstring;}
		    set { connectionstring = value;}
	    }
	        
        #endregion

        #region IUngdungDAL members
        #region Get
        public void Get(Ungdung ungdung)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@ung_dung_id", ungdung.Ung_dung_id);
                    using (SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "P_Ung_dung_Get", par))
                    {
                        while (dr.Read())
                        {
                            ungdung.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                            ungdung.Ma = dr["ma"].ToString();
                            ungdung.Ten = dr["ten"].ToString();
                            ungdung.Mieuta = dr["mieuta"].ToString();
                            ungdung.Icon = dr["icon"].ToString();
                            ungdung.Bundle_id = dr["bundle_id"].ToString();
                            ungdung.Trangthai = int.Parse(dr["trangthai"].ToString());
                            ungdung.Ngaytao = dr["ngaytao"].ToString();
                            ungdung.Ngayduyet = dr["ngayduyet"].ToString();
                            ungdung.Nguoitao = dr["nguoitao"].ToString();
                            ungdung.Nguoiduyet = dr["nguoiduyet"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Add
        public int Add(Ungdung ungdung)
        {
            int rowNo = -1;
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[11];
                    par[0] = new SqlParameter("@ung_dung_id", ungdung.Ung_dung_id);
                    par[0].Direction = ParameterDirection.Output;
                    par[1] = new SqlParameter("@ma", ungdung.Ma);
                    par[2] = new SqlParameter("@ten", ungdung.Ten);
                    par[3] = new SqlParameter("@mieuta", ungdung.Mieuta);
                    par[4] = new SqlParameter("@icon", ungdung.Icon);
                    par[5] = new SqlParameter("@bundle_id", ungdung.Bundle_id);
                    par[6] = new SqlParameter("@trangthai", ungdung.Trangthai);
                    par[7] = new SqlParameter("@ngaytao", ungdung.Ngaytao);
                    par[8] = new SqlParameter("@ngayduyet", ungdung.Ngayduyet);
                    par[9] = new SqlParameter("@nguoitao", ungdung.Nguoitao);
                    par[10] = new SqlParameter("@nguoiduyet", ungdung.Nguoiduyet);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_ung_dung_Insert", par);
                    rowNo = int.Parse(par[0].Value.ToString());
                }
                
            }
            catch (Exception ex)
            {
                throw;
            }
            return rowNo;
        }
        #endregion

        #region Delete
        public void Delete(Ungdung ungdung)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@ung_dung_id", ungdung.Ung_dung_id);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_ung_dung_Delete", par);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Update
        public void Update(Ungdung ungdung)
        {
            try
            {
                using(SqlConnection conn= new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[11];
                    par[0] = new SqlParameter("@ung_dung_id", ungdung.Ung_dung_id);
                    par[1] = new SqlParameter("@ma", ungdung.Ma);
                    par[2] = new SqlParameter("@ten", ungdung.Ten);
                    par[3] = new SqlParameter("@mieuta", ungdung.Mieuta);
                    par[4] = new SqlParameter("@icon", ungdung.Icon);
                    par[5] = new SqlParameter("@bundle_id", ungdung.Bundle_id);
                    par[6] = new SqlParameter("@trangthai", ungdung.Trangthai);
                    par[7] = new SqlParameter("@ngaytao", ungdung.Ngaytao);
                    par[8] = new SqlParameter("@ngayduyet", ungdung.Ngayduyet);
                    par[9] = new SqlParameter("@nguoitao", ungdung.Nguoitao);
                    par[10] = new SqlParameter("@nguoiduyet", ungdung.Nguoiduyet);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_ung_dung_Update", par);
                }
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region GetAll
        public List<Ungdung> GetAll(int currentPage, int pageSize, out int total, string ten, string column_sort, string type_sort)
        {
            try
            {
                List<Ungdung> soList = new List<Ungdung>();
                using (SqlConnection con = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[6];
                    par[0] = new SqlParameter("@currentpage", currentPage);
                    par[1] = new SqlParameter("@pagesize", pageSize);
                    par[2] = new SqlParameter("@total", SqlDbType.Int);
                    par[2].Direction = ParameterDirection.Output;
                    par[3] = new SqlParameter("@solution_name", ten);
                    par[4] = new SqlParameter("@column_sort", column_sort);
                    par[5] = new SqlParameter("@type_sort", type_sort);

                    using (SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "P_Solutions_GetAll", par))
                    {
                        while (dr.Read())
                        {
                            Ungdung ungdung = new Ungdung();
                            ungdung.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                            ungdung.Ma = dr["ma"].ToString();
                            ungdung.Ten = dr["ten"].ToString();
                            ungdung.Mieuta = dr["mieuta"].ToString();
                            ungdung.Icon = dr["icon"].ToString();
                            ungdung.Bundle_id = dr["bundle_id"].ToString();
                            ungdung.Trangthai = int.Parse(dr["trangthai"].ToString());
                            ungdung.Ngaytao = dr["ngaytao"].ToString();
                            ungdung.Ngayduyet = dr["ngayduyet"].ToString();
                            ungdung.Nguoitao = dr["nguoitao"].ToString();
                            ungdung.Nguoiduyet = dr["nguoiduyet"].ToString();
                        }
                    }
                    total = int.Parse(par[2].Value.ToString());
                }
                return soList;
            }
            catch (Exception ex) { throw; }
        }
        #endregion
        #endregion
    }
}
